package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.Verify;
import com.example.demo.expcetion.ApiException;
import com.example.demo.service.VerifyService;
import com.example.demo.vo.request.UpdatePasswordRequest;
import com.example.demo.vo.response.ResultEntity;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/verify/user")
public class VerifyController {
	@Autowired
    private VerifyService verifyService;
	
	/**
	   *   发送短信
	 * @param phone
	 * @return
	 */
	@GetMapping("/sendSMS")
	public ResultEntity<Void> sendSMS(@RequestParam("phone") String phone) {
		try {
			verifyService.sendMsg(phone);	
		} catch (ApiException e) {
			return new ResultEntity<Void>(e.getCode(),e.getMessage());
		}
    	return new ResultEntity<Void>();
    }

	/**
	 *  修改密码
	 * @return
	 */
	@PostMapping("/updatePassword")
	public ResultEntity<String> updatePassword(@RequestBody UpdatePasswordRequest updatePasswordRequest) {
		try {
			verifyService.updatePassword(updatePasswordRequest);
		} catch (ApiException e) {
			return new ResultEntity<>(e.getCode(),e.getMessage());
		}
		return new ResultEntity<String>();
	}
	
	@PostMapping
	@ApiOperation("添加redis")
	/**
	 * 无RequestBody 以post传统方式传参
	 */
	public String add(@RequestBody Verify phone) {
		verifyService.save(phone);
		return "success";
	}
}



